﻿using System;
using System.Web.Mvc;
using Easy.Common;
using Easy.ServiceLocator;

namespace Easy.AspNetMvc
{
    public class IocControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                CheckHelper.NotNull(controllerType, "controllerType", "MVC 控制器类型不能为空！");
            }

            if (!typeof(IController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException(string.Format(Resource.NotAssignableFrom, controllerType.Name, "IController"), "controllerType");
            }

            return EasyServiceLocator.GetInstance(controllerType) as IController;
        }
    }
}
